home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume3 / date < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  7.6 KB

  1. From: DIDELOT Andre <talcott!seismo!mcvax!cui!andre>
  2. Subject: date - formatted date program
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 3, Issue 122
  7. Submitted by: DIDELOT Andre <talcott!seismo!mcvax!cui!andre>
  8.  
  9.  
  10. ---8<-------CUT--HERE------8<-------CUT--HERE-------8<-------CUT--HERE------8<-
  11. #!/bin/sh
  12. # This is a shell archive, meaning:
  13. # 1. Remove everything above the #!/bin/sh line.
  14. # 2. Save the resulting text in a file.
  15. # 3. Execute the file with sh (not csh) to create the files:
  16. #          date.1
  17. #          date.c
  18. #
  19. # Created: Tue Feb 11 14:13:38 MET 1986
  20. # Creator: DIDELOT Andre (University of Geneva)
  21.  
  22. export PATH
  23. PATH=/bin:$PATH
  24.  
  25. if [ -f 'date.1' ]
  26. then
  27.     echo ***WARNING "'date.1'" exists, not remplaced
  28. else
  29.     echo extracting "'date.1'" 1>&2
  30.     sed -e 's/^X//' > 'date.1' <<'---EOF(date.1)---'
  31. X.TH DATE l "6 August 1984"
  32. X.UC 4
  33. X.SH NAME
  34. Xdate \- formatted output of current date
  35. X.SH SYNOPSIS
  36. X.B date
  37. X[ 
  38. X.B \-lf
  39. X]
  40. X[ [\-]
  41. X.B format variables
  42. X]
  43. X.SH DESCRIPTION
  44. XWithout any argument,
  45. X.I date
  46. Xoutput use ASCII format.
  47. XThe
  48. X.B \-f
  49. Xoption stand for french output.
  50. XThe
  51. X.B \-l
  52. Xoption stand for long output, either in english or in french.
  53. X.PP
  54. XThe string
  55. X.I format
  56. Xcontains a format like that used by the C subroutine
  57. X.I printf
  58. X; all special char conventions are recognized.
  59. XThe string would have better to be quoted to prevent any further shell
  60. Xinterpretation.
  61. XAn optionnal single \- allow use of formats beginning with a \-.
  62. X.PP
  63. X.I Variables
  64. Xis a list of single letters, either comma or blank separated, or even
  65. Xconcatened alltogether; valid names and their meaning are :
  66. X.TP 10
  67. Xd, j
  68. Xday of the week.
  69. X.TP 10
  70. XM
  71. Xmonth.
  72. X.TP 10
  73. XD, J
  74. Xday of the month.
  75. X.TP 10
  76. Xh
  77. Xhours.
  78. X.TP 10
  79. Xm
  80. Xminutes.
  81. X.TP 10
  82. Xs
  83. Xseconds.
  84. X.TP 10
  85. XY, A
  86. Xyear.
  87. X.SH SEE ALSO
  88. Xctime, localtime, gmtime, asctime, timezone (3)
  89. X.SH AUTHOR
  90. XA. Didelot
  91. X.SH BUGS
  92. XNo way to specify letter's case using format.
  93. ---EOF(date.1)---
  94.     size=`wc -c date.1 | awk '{print \$1}'`
  95.     if [ 1050 -ne $size ]
  96.     then
  97.         echo ***WARNING "'date.1'" bad checksum
  98.     fi
  99. fi
  100.  
  101. if [ -f 'date.c' ]
  102. then
  103.     echo ***WARNING "'date.c'" exists, not remplaced
  104. else
  105.     echo extracting "'date.c'" 1>&2
  106.     sed -e 's/^X//' > 'date.c' <<'---EOF(date.c)---'
  107. X#include <stdio.h>
  108. X#include <sys/time.h>
  109. X
  110. Xchar *dayshort[] = {
  111. X    "Mon",
  112. X    "Tue",
  113. X    "Wed",
  114. X    "Thu",
  115. X    "Fri",
  116. X    "Sat",
  117. X    "Sun"
  118. X};
  119. X
  120. Xchar *monthshort[] = {
  121. X    "Jan",
  122. X    "Feb",
  123. X    "Mar",
  124. X    "Apr",
  125. X    "May",
  126. X    "Jun",
  127. X    "Jul",
  128. X    "Aug",
  129. X    "Sep",
  130. X    "Oct",
  131. X    "Nov",
  132. X    "Dec"
  133. X};
  134. X
  135. Xchar *days[] = {
  136. X    "monday",
  137. X    "tuesday",
  138. X    "wednesday",
  139. X    "thursday",
  140. X    "friday",
  141. X    "saturday",
  142. X    "sunday"
  143. X};
  144. X
  145. Xchar *months[] = {
  146. X    "january",
  147. X    "february",
  148. X    "march",
  149. X    "april",
  150. X    "may",
  151. X    "june",
  152. X    "july",
  153. X    "august",
  154. X    "september",
  155. X    "october",
  156. X    "november",
  157. X    "december"
  158. X};
  159. X
  160. Xchar *jrs[] = {
  161. X    "lun",
  162. X    "mar",
  163. X    "mer",
  164. X    "jeu",
  165. X    "ven",
  166. X    "sam",
  167. X    "dim"
  168. X};
  169. X
  170. Xchar *ms[] = {
  171. X    "jan",
  172. X    "fev",
  173. X    "mar",
  174. X    "avr",
  175. X    "mai",
  176. X    "juin",
  177. X    "juil",
  178. X    "aou",
  179. X    "sep",
  180. X    "oct",
  181. X    "nov",
  182. X    "dec"
  183. X};
  184. X
  185. Xchar *jours[] = {
  186. X    "lundi",
  187. X    "mardi",
  188. X    "mercredi",
  189. X    "jeudi",
  190. X    "vendredi",
  191. X    "samedi",
  192. X    "dimanche"
  193. X};
  194. X
  195. Xchar *mois[] = {
  196. X    "janvier",
  197. X    "fevrier",
  198. X    "mars",
  199. X    "avril",
  200. X    "mai",
  201. X    "juin",
  202. X    "juillet",
  203. X    "aout",
  204. X    "septembre",
  205. X    "octobre",
  206. X    "novembre",
  207. X    "decembre"
  208. X};
  209. X
  210. Xmain( argc, argv)
  211. X
  212. Xint    argc;
  213. Xchar    **argv;
  214. X
  215. X{    long    l;
  216. X
  217. X    int     i, j,
  218. X        fflg, lflg,
  219. X        iday, imonth;
  220. X
  221. X    char    *word[7],
  222. X        format[80],
  223. X        *command,
  224. X        *date,
  225. X        day[15],
  226. X        month[15],
  227. X        daymonth[3],
  228. X        hour[3],
  229. X        minute[3],
  230. X        second[3],
  231. X        year[5];
  232. X
  233. X/* get date                */
  234. X
  235. X    time(&l);
  236. X    date = ctime(&l);
  237. X
  238. X/* print date and exit, if no argument    */
  239. X
  240. X    if (argc == 1) {
  241. X        printf( "%s", date);
  242. X        exit(0);
  243. X    }
  244. X
  245. X/* save name of command            */
  246. X
  247. X    command = argv[0];
  248. X
  249. X/* get options if any            */
  250. X
  251. X    argc--;    argv++;
  252. X    lflg = fflg = 0;
  253. X    while(argv[0][0] == '-' && argv[0][1]) {
  254. X        for( i=1; argv[0][i]; i++)
  255. X        switch(argv[0][i]) {
  256. X
  257. X        case 'f':
  258. X            fflg++;
  259. X            break;
  260. X
  261. X        case 'l':
  262. X            lflg++;
  263. X            break;
  264. X
  265. X        default:
  266. X            fprintf( stderr,
  267. X            "%s : bad option %c\n", command, argv[0][i]);
  268. X        }
  269. X        argc--; argv++;
  270. X    }
  271. X    if (argv[0][0] == '-') {
  272. X        argc--; argv++;
  273. X    }
  274. X
  275. X/* initialize day, month, ...        */
  276. X
  277. X    substrcpy( date,  0,  2, day);
  278. X    substrcpy( date,  4,  6, month);
  279. X    if (date[8] == ' ') {
  280. X        daymonth[0] = date[9];
  281. X        daymonth[1] = '\0';
  282. X        }
  283. X    else
  284. X        substrcpy( date,  8,  9, daymonth);
  285. X    substrcpy( date, 11, 12, hour);
  286. X    substrcpy( date, 14, 15, minute);
  287. X    substrcpy( date, 17, 18, second);
  288. X    substrcpy( date, 20, 23, year);
  289. X
  290. X
  291. X/* compute iday, imonth            */
  292. X
  293. X    for( iday=0; iday<7; iday++) 
  294. X        if (!strncmp( dayshort[iday], day, 3)) break;
  295. X    for( imonth=0; imonth<12; imonth++) 
  296. X        if (!strncmp( monthshort[imonth], month, 3)) break;
  297. X
  298. X/* look for french and/or long output    */
  299. X
  300. X    if (fflg)
  301. X        if (lflg) {
  302. X        strcpy( day,   jours[iday]);
  303. X        strcpy( month, mois[imonth]);
  304. X        }
  305. X        else {
  306. X        strcpy( day,   jrs[iday]);
  307. X        strcpy( month, ms[imonth]);
  308. X        }
  309. X    else
  310. X        if (lflg) {
  311. X        strcpy( day,   days[iday]);
  312. X        strcpy( month, months[imonth]);
  313. X        }
  314. X
  315. X/* check number of arguments        */
  316. X
  317. X    if (argc == 1 || argc > 8) {
  318. X        fprintf( stderr,
  319. X        "%s : wrong number of arguments\n", command);
  320. X        exit(1);
  321. X    }
  322. X
  323. X    if (argc) {
  324. X
  325. X/* translate C convention like \n, \t, ...
  326. X   into single char.            */
  327. X
  328. X        for ( i=0, j=0; argv[0][i]; i++, j++) {
  329. X        format[j] = argv[0][i];
  330. X        if (argv[0][i] == '\\') 
  331. X            switch (argv[0][++i]) {
  332. X
  333. X            case 'n':
  334. X                format[j] = '\n';
  335. X                break;
  336. X
  337. X            case 't':
  338. X                format[j] = '\t';
  339. X                break;
  340. X
  341. X            case 'b':
  342. X                format[j] = '\b';
  343. X                break;
  344. X
  345. X            case 'r':
  346. X                format[j] = '\r';
  347. X                break;
  348. X
  349. X            case 'f':
  350. X                format[j] = '\f';
  351. X                break;
  352. X
  353. X            case '\\':
  354. X                break;
  355. X
  356. X            default:
  357. X                format[++j] = argv[0][i];
  358. X            }
  359. X        }
  360. X        format[j] = '\0';
  361. X        argc--; argv++;
  362. X
  363. X/* order arguments            */
  364. X
  365. X        for( i=0; i<7; i++) word[i] = '\0';
  366. X        j = 0;
  367. X        while (argc) {
  368. X        for( i=0; argv[0][i]; i++)
  369. X            switch(argv[0][i]) {
  370. X
  371. X            case 'd':
  372. X            case 'j':
  373. X                word[j++] = day;
  374. X                break;
  375. X
  376. X            case 'M':
  377. X                word[j++] = month;
  378. X                break;
  379. X
  380. X            case 'D':
  381. X            case 'J':
  382. X                word[j++] = daymonth;
  383. X                break;
  384. X
  385. X            case 'h':
  386. X                word[j++] = hour;
  387. X                break;
  388. X
  389. X            case 'm':
  390. X                word[j++] = minute;
  391. X                break;
  392. X
  393. X            case 's':
  394. X                word[j++] = second;
  395. X                break;
  396. X
  397. X            case 'A':    
  398. X            case 'Y':    
  399. X                word[j++] = year;
  400. X                break;
  401. X
  402. X            case ',':
  403. X                break;
  404. X
  405. X            default:    
  406. X                fprintf( stderr,
  407. X                "%s : bad argument %c\n", command, argv[0][i]);
  408. X            }
  409. X        argc--;    argv++;
  410. X        }
  411. X
  412. X/* output date according to format    */
  413. X
  414. X        printf( format, word[0], word[1], word[2],
  415. X            word[3], word[4], word[5], word[6]);
  416. X        }
  417. X
  418. X    else {
  419. X
  420. X        strcpy( format, "%s %s %s %s   %s:%s:%s\n");
  421. X        if (fflg)
  422. X        printf( format, day, daymonth, month, year, hour, minute, second);
  423. X        else
  424. X        printf( format, day, month, daymonth, year, hour, minute, second);
  425. X    }
  426. X}
  427. X
  428. X
  429. X/* copy string str1[n..m] into str2    */
  430. X
  431. Xsubstrcpy( str1, n, m, str2)
  432. X
  433. Xint    n, m;
  434. Xchar    *str1, *str2;
  435. X
  436. X{
  437. X    m++;
  438. X    if ( n >= 0 && m >= n ) {
  439. X        while( n-- && *str1++ && m--);
  440. X        while( m-- && ( *str2++ = *str1++ ));
  441. X    }
  442. X    *str2='\0';
  443. X}
  444. ---EOF(date.c)---
  445.     size=`wc -c date.c | awk '{print \$1}'`
  446.     if [ 4636 -ne $size ]
  447.     then
  448.         echo ***WARNING "'date.c'" bad checksum
  449.     fi
  450. fi
  451. ---8<-------CUT--HERE------8<-------CUT--HERE-------8<-------CUT--HERE------8<-
  452.     Andre DIDELOT                CHUNET: andre@cui.unige.chunet
  453.  
  454. MAIL:    Centre Universitaire d'Informatique    UUCP:    mcvax!cernvax!cui!andre
  455.     Universite de Geneve                mcvax!cernvax!cui!root
  456.     Rue du General Dufour 24
  457.     CH - 1211 GENEVE 4            BITNET: DIDELOT@CGEUGE51
  458.     SWITZERLAND                    SYSTEM@CGEUGE51
  459.  
  460.  
  461.